<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="分组ID" prop="groupId">
        <el-input v-model="formData.groupId" placeholder="请输入分组ID" />
      </el-form-item>
      <el-form-item label="分组类型" prop="groupType">
        <el-select v-model="formData.groupType" placeholder="请选择分组类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="分组封面" prop="groupLogo">
        <el-input v-model="formData.groupLogo" placeholder="请输入分组封面" />
      </el-form-item>
      <el-form-item label="会话用户ID" prop="byUserId">
        <el-input v-model="formData.byUserId" placeholder="请输入会话用户ID" />
      </el-form-item>
      <el-form-item label="会话所属用户ID" prop="userId">
        <el-input v-model="formData.userId" placeholder="请输入会话所属用户ID" />
      </el-form-item>
      <el-form-item label="最后聊天时间" prop="lastMessageTime">
        <el-date-picker
          v-model="formData.lastMessageTime"
          type="date"
          value-format="x"
          placeholder="选择最后聊天时间"
        />
      </el-form-item>
      <el-form-item label="最后聊天内容" prop="lastMessageContent">
        <Editor v-model="formData.lastMessageContent" height="150px" />
      </el-form-item>
      <el-form-item label="最后发送的消息类型" prop="lastMessageContentType">
        <el-select v-model="formData.lastMessageContentType" placeholder="请选择最后发送的消息类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="最后发送的消息是否已读" prop="lastMessageReadStatus">
        <el-radio-group v-model="formData.lastMessageReadStatus">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="置顶" prop="pinned">
        <el-input v-model="formData.pinned" placeholder="请输入置顶" />
      </el-form-item>
      <el-form-item label="未读消息数" prop="unreadMessageCount">
        <el-input v-model="formData.unreadMessageCount" placeholder="请输入未读消息数" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { MemberConversationApi, MemberConversationVO } from '@/api/im/member/conversation'

/** IM会话 表单 */
defineOptions({ name: 'MemberConversationForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  groupId: undefined,
  groupType: undefined,
  groupLogo: undefined,
  byUserId: undefined,
  userId: undefined,
  lastMessageTime: undefined,
  lastMessageContent: undefined,
  lastMessageContentType: undefined,
  lastMessageReadStatus: undefined,
  pinned: undefined,
  unreadMessageCount: undefined,
})
const formRules = reactive({
  groupId: [{ required: true, message: '分组ID不能为空', trigger: 'blur' }],
  groupType: [{ required: true, message: '分组类型不能为空', trigger: 'change' }],
  byUserId: [{ required: true, message: '会话用户ID不能为空', trigger: 'blur' }],
  userId: [{ required: true, message: '会话所属用户ID不能为空', trigger: 'blur' }],
  lastMessageTime: [{ required: true, message: '最后聊天时间不能为空', trigger: 'blur' }],
  lastMessageContentType: [{ required: true, message: '最后发送的消息类型不能为空', trigger: 'change' }],
  unreadMessageCount: [{ required: true, message: '未读消息数不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await MemberConversationApi.getMemberConversation(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as MemberConversationVO
    if (formType.value === 'create') {
      await MemberConversationApi.createMemberConversation(data)
      message.success(t('common.createSuccess'))
    } else {
      await MemberConversationApi.updateMemberConversation(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    groupId: undefined,
    groupType: undefined,
    groupLogo: undefined,
    byUserId: undefined,
    userId: undefined,
    lastMessageTime: undefined,
    lastMessageContent: undefined,
    lastMessageContentType: undefined,
    lastMessageReadStatus: undefined,
    pinned: undefined,
    unreadMessageCount: undefined,
  }
  formRef.value?.resetFields()
}
</script>
